home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / gdb / findvar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  27.6 KB  |  1,047 lines

  1. /* Find a variable's value in memory, for GDB, the GNU debugger.
  2.    Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "symtab.h"
  22. #include "gdbtypes.h"
  23. #include "frame.h"
  24. #include "value.h"
  25. #include "gdbcore.h"
  26. #include "inferior.h"
  27. #include "target.h"
  28.  
  29. /* Basic byte-swapping routines.  GDB has needed these for a long time...
  30.    All extract a target-format integer at ADDR which is LEN bytes long.  */
  31.  
  32. #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
  33.   /* 8 bit characters are a pretty safe assumption these days, so we
  34.      assume it throughout all these swapping routines.  If we had to deal with
  35.      9 bit characters, we would need to make len be in bits and would have
  36.      to re-write these routines...  */
  37.   you lose
  38. #endif
  39.  
  40. LONGEST
  41. extract_signed_integer (addr, len)
  42.      PTR addr;
  43.      int len;
  44. {
  45.   LONGEST retval;
  46.   unsigned char *p;
  47.   unsigned char *startaddr = (unsigned char *)addr;
  48.   unsigned char *endaddr = startaddr + len;
  49.  
  50.   if (len > sizeof (LONGEST))
  51.     error ("\
  52. That operation is not available on integers of more than %d bytes.",
  53.        sizeof (LONGEST));
  54.  
  55.   /* Start at the most significant end of the integer, and work towards
  56.      the least significant.  */
  57. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  58.   p = startaddr;
  59. #else
  60.   p = endaddr - 1;
  61. #endif
  62.   /* Do the sign extension once at the start.  */
  63.   retval = ((LONGEST)*p ^ 0x80) - 0x80;
  64. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  65.   for (++p; p < endaddr; ++p)
  66. #else
  67.   for (--p; p >= startaddr; --p)
  68. #endif
  69.     {
  70.       retval = (retval << 8) | *p;
  71.     }
  72.   return retval;
  73. }
  74.  
  75. unsigned LONGEST
  76. extract_unsigned_integer (addr, len)
  77.      PTR addr;
  78.      int len;
  79. {
  80.   unsigned LONGEST retval;
  81.   unsigned char *p;
  82.   unsigned char *startaddr = (unsigned char *)addr;
  83.   unsigned char *endaddr = startaddr + len;
  84.  
  85.   if (len > sizeof (unsigned LONGEST))
  86.     error ("\
  87. That operation is not available on integers of more than %d bytes.",
  88.        sizeof (unsigned LONGEST));
  89.  
  90.   /* Start at the most significant end of the integer, and work towards
  91.      the least significant.  */
  92.   retval = 0;
  93. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  94.   for (p = startaddr; p < endaddr; ++p)
  95. #else
  96.   for (p = endaddr - 1; p >= startaddr; --p)
  97. #endif
  98.     {
  99.       retval = (retval << 8) | *p;
  100.     }
  101.   return retval;
  102. }
  103.  
  104. CORE_ADDR
  105. extract_address (addr, len)
  106.      PTR addr;
  107.      int len;
  108. {
  109.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  110.      whether we want this to be true eventually.  */
  111.   return extract_unsigned_integer (addr, len);
  112. }
  113.  
  114. void
  115. store_signed_integer (addr, len, val)
  116.      PTR addr;
  117.      int len;
  118.      LONGEST val;
  119. {
  120.   unsigned char *p;
  121.   unsigned char *startaddr = (unsigned char *)addr;
  122.   unsigned char *endaddr = startaddr + len;
  123.  
  124.   /* Start at the least significant end of the integer, and work towards
  125.      the most significant.  */
  126. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  127.   for (p = endaddr - 1; p >= startaddr; --p)
  128. #else
  129.   for (p = startaddr; p < endaddr; ++p)
  130. #endif
  131.     {
  132.       *p = val & 0xff;
  133.       val >>= 8;
  134.     }
  135. }
  136.  
  137. void
  138. store_unsigned_integer (addr, len, val)
  139.      PTR addr;
  140.      int len;
  141.      unsigned LONGEST val;
  142. {
  143.   unsigned char *p;
  144.   unsigned char *startaddr = (unsigned char *)addr;
  145.   unsigned char *endaddr = startaddr + len;
  146.  
  147.   /* Start at the least significant end of the integer, and work towards
  148.      the most significant.  */
  149. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  150.   for (p = endaddr - 1; p >= startaddr; --p)
  151. #else
  152.   for (p = startaddr; p < endaddr; ++p)
  153. #endif
  154.     {
  155.       *p = val & 0xff;
  156.       val >>= 8;
  157.     }
  158. }
  159.  
  160. void
  161. store_address (addr, len, val)
  162.      PTR addr;
  163.      int len;
  164.      CORE_ADDR val;
  165. {
  166.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  167.      whether we want this to be true eventually.  */
  168.   store_unsigned_integer (addr, len, (LONGEST)val);
  169. }
  170.  
  171. /* Swap LEN bytes at BUFFER between target and host byte-order.  This is
  172.    the wrong way to do byte-swapping because it assumes that you have a way
  173.    to have a host variable of exactly the right size.  Once extract_floating
  174.    and store_floating have been fixed, this can go away.  */
  175. #if TARGET_BYTE_ORDER == HOST_BYTE_ORDER
  176. #define SWAP_TARGET_AND_HOST(buffer,len)
  177. #else /* Target and host byte order differ.  */
  178. #define SWAP_TARGET_AND_HOST(buffer,len) \
  179.   {                                                                                             \
  180.     char tmp;                                 \
  181.     char *p = (char *)(buffer);                         \
  182.     char *q = ((char *)(buffer)) + len - 1;                    \
  183.     for (; p < q; p++, q--)                          \
  184.       {                                     \
  185.         tmp = *q;                             \
  186.         *q = *p;                             \
  187.         *p = tmp;                             \
  188.       }                                     \
  189.   }
  190. #endif /* Target and host byte order differ.  */
  191.  
  192. /* There are many problems with floating point cross-debugging.
  193.  
  194.    1.  These routines only handle byte-swapping, not conversion of
  195.    formats.  So if host is IEEE floating and target is VAX floating,
  196.    or vice-versa, it loses.  This means that we can't (yet) use these
  197.    routines for extendeds.  Extendeds are handled by
  198.    REGISTER_CONVERTIBLE.  What we want is a fixed version of
  199.    ieee-float.c (the current version can't deal with single or double,
  200.    and I suspect it is probably broken for some extendeds too).
  201.  
  202.    2.  We can't deal with it if there is more than one floating point
  203.    format in use.  This has to be fixed at the unpack_double level.
  204.  
  205.    3.  We probably should have a LONGEST_DOUBLE or DOUBLEST or whatever
  206.    we want to call it which is long double where available.  */
  207.  
  208. double
  209. extract_floating (addr, len)
  210.      PTR addr;
  211.      int len;
  212. {
  213.   if (len == sizeof (float))
  214.     {
  215.       float retval;
  216.       memcpy (&retval, addr, sizeof (retval));
  217.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  218.       return retval;
  219.     }
  220.   else if (len == sizeof (double))
  221.     {
  222.       double retval;
  223.       memcpy (&retval, addr, sizeof (retval));
  224.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  225.       return retval;
  226.     }
  227.   else
  228.     {
  229.       error ("Can't deal with a floating point number of %d bytes.", len);
  230.     }
  231. }
  232.  
  233. void
  234. store_floating (addr, len, val)
  235.      PTR addr;
  236.      int len;
  237.      double val;
  238. {
  239.   if (len == sizeof (float))
  240.     {
  241.       float floatval = val;
  242.       SWAP_TARGET_AND_HOST (&floatval, sizeof (floatval));
  243.       memcpy (addr, &floatval, sizeof (floatval));
  244.     }
  245.   else if (len == sizeof (double))
  246.     {
  247.       SWAP_TARGET_AND_HOST (&val, sizeof (val));
  248.       memcpy (addr, &val, sizeof (val));
  249.     }
  250.   else
  251.     {
  252.       error ("Can't deal with a floating point number of %d bytes.", len);
  253.     }
  254. }
  255.  
  256. #if !defined (GET_SAVED_REGISTER)
  257.  
  258. /* Return the address in which frame FRAME's value of register REGNUM
  259.    has been saved in memory.  Or return zero if it has not been saved.
  260.    If REGNUM specifies the SP, the value we return is actually
  261.    the SP value, not an address where it was saved.  */
  262.  
  263. CORE_ADDR
  264. find_saved_register (frame, regnum)
  265.      FRAME frame;
  266.      int regnum;
  267. {
  268.   struct frame_info *fi;
  269.   struct frame_saved_regs saved_regs;
  270.  
  271.   register FRAME frame1 = 0;
  272.   register CORE_ADDR addr = 0;
  273.  
  274.   if (frame == 0)        /* No regs saved if want current frame */
  275.     return 0;
  276.  
  277. #ifdef HAVE_REGISTER_WINDOWS
  278.   /* We assume that a register in a register window will only be saved
  279.      in one place (since the name changes and/or disappears as you go
  280.      towards inner frames), so we only call get_frame_saved_regs on
  281.      the current frame.  This is directly in contradiction to the
  282.      usage below, which assumes that registers used in a frame must be
  283.      saved in a lower (more interior) frame.  This change is a result
  284.      of working on a register window machine; get_frame_saved_regs
  285.      always returns the registers saved within a frame, within the
  286.      context (register namespace) of that frame. */
  287.  
  288.   /* However, note that we don't want this to return anything if
  289.      nothing is saved (if there's a frame inside of this one).  Also,
  290.      callers to this routine asking for the stack pointer want the
  291.      stack pointer saved for *this* frame; this is returned from the
  292.      next frame.  */
  293.      
  294.  
  295.   if (REGISTER_IN_WINDOW_P(regnum))
  296.     {
  297.       frame1 = get_next_frame (frame);
  298.       if (!frame1) return 0;    /* Registers of this frame are
  299.                    active.  */
  300.       
  301.       /* Get the SP from the next frame in; it will be this
  302.      current frame.  */
  303.       if (regnum != SP_REGNUM)
  304.     frame1 = frame;    
  305.       
  306.       fi = get_frame_info (frame1);
  307.       get_frame_saved_regs (fi, &saved_regs);
  308.       return saved_regs.regs[regnum];    /* ... which might be zero */
  309.     }
  310. #endif /* HAVE_REGISTER_WINDOWS */
  311.  
  312.   /* Note that this next routine assumes that registers used in
  313.      frame x will be saved only in the frame that x calls and
  314.      frames interior to it.  This is not true on the sparc, but the
  315.      above macro takes care of it, so we should be all right. */
  316.   while (1)
  317.     {
  318.       QUIT;
  319.       frame1 = get_prev_frame (frame1);
  320.       if (frame1 == 0 || frame1 == frame)
  321.     break;
  322.       fi = get_frame_info (frame1);
  323.       get_frame_saved_regs (fi, &saved_regs);
  324.       if (saved_regs.regs[regnum])
  325.     addr = saved_regs.regs[regnum];
  326.     }
  327.  
  328.   return addr;
  329. }
  330.  
  331. /* Find register number REGNUM relative to FRAME and put its (raw,
  332.    target format) contents in *RAW_BUFFER.  Set *OPTIMIZED if the
  333.    variable was optimized out (and thus can't be fetched).  Set *LVAL
  334.    to lval_memory, lval_register, or not_lval, depending on whether
  335.    the value was fetched from memory, from a register, or in a strange
  336.    and non-modifiable way (e.g. a frame pointer which was calculated
  337.    rather than fetched).  Set *ADDRP to the address, either in memory
  338.    on as a REGISTER_BYTE offset into the registers array.
  339.  
  340.    Note that this implementation never sets *LVAL to not_lval.  But
  341.    it can be replaced by defining GET_SAVED_REGISTER and supplying
  342.    your own.
  343.  
  344.    The argument RAW_BUFFER must point to aligned memory.  */
  345.  
  346. void
  347. get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
  348.      char *raw_buffer;
  349.      int *optimized;
  350.      CORE_ADDR *addrp;
  351.      FRAME frame;
  352.      int regnum;
  353.      enum lval_type *lval;
  354. {
  355.   CORE_ADDR addr;
  356.   /* Normal systems don't optimize out things with register numbers.  */
  357.   if (optimized != NULL)
  358.     *optimized = 0;
  359.   addr = find_saved_register (frame, regnum);
  360.   if (addr != 0)
  361.     {
  362.       if (lval != NULL)
  363.     *lval = lval_memory;
  364.       if (regnum == SP_REGNUM)
  365.     {
  366.       if (raw_buffer != NULL)
  367.         {
  368.           /* Put it back in target format.  */
  369.           store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), addr);
  370.         }
  371.       if (addrp != NULL)
  372.         *addrp = 0;
  373.       return;
  374.     }
  375.       if (raw_buffer != NULL)
  376.     read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
  377.     }
  378.   else
  379.     {
  380.       if (lval != NULL)
  381.     *lval = lval_register;
  382.       addr = REGISTER_BYTE (regnum);
  383.       if (raw_buffer != NULL)
  384.     read_register_gen (regnum, raw_buffer);
  385.     }
  386.   if (addrp != NULL)
  387.     *addrp = addr;
  388. }
  389. #endif /* GET_SAVED_REGISTER.  */
  390.  
  391. /* Copy the bytes of register REGNUM, relative to the current stack frame,
  392.    into our memory at MYADDR, in target byte order.
  393.    The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
  394.  
  395.    Returns 1 if could not be read, 0 if could.  */
  396.  
  397. int
  398. read_relative_register_raw_bytes (regnum, myaddr)
  399.      int regnum;
  400.      char *myaddr;
  401. {
  402.   int optim;
  403.   if (regnum == FP_REGNUM && selected_frame)
  404.     {
  405.       /* Put it back in target format.  */
  406.       store_address (myaddr, REGISTER_RAW_SIZE(FP_REGNUM),
  407.              FRAME_FP(selected_frame));
  408.       return 0;
  409.     }
  410.  
  411.   get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
  412.                       regnum, (enum lval_type *)NULL);
  413.   return optim;
  414. }
  415.  
  416. /* Return a `value' with the contents of register REGNUM
  417.    in its virtual format, with the type specified by
  418.    REGISTER_VIRTUAL_TYPE.  */
  419.  
  420. value
  421. value_of_register (regnum)
  422.      int regnum;
  423. {
  424.   CORE_ADDR addr;
  425.   int optim;
  426.   register value reg_val;
  427.   char raw_buffer[MAX_REGISTER_RAW_SIZE];
  428.   enum lval_type lval;
  429.  
  430.   get_saved_register (raw_buffer, &optim, &addr,
  431.               selected_frame, regnum, &lval);
  432.  
  433.   reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
  434.  
  435.   /* Convert raw data to virtual format if necessary.  */
  436.  
  437. #ifdef REGISTER_CONVERTIBLE
  438.   if (REGISTER_CONVERTIBLE (regnum))
  439.     {
  440.       REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
  441.                    raw_buffer, VALUE_CONTENTS_RAW (reg_val));
  442.     }
  443.   else
  444. #endif
  445.     memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
  446.         REGISTER_RAW_SIZE (regnum));
  447.   VALUE_LVAL (reg_val) = lval;
  448.   VALUE_ADDRESS (reg_val) = addr;
  449.   VALUE_REGNO (reg_val) = regnum;
  450.   VALUE_OPTIMIZED_OUT (reg_val) = optim;
  451.   return reg_val;
  452. }
  453.  
  454. /* Low level examining and depositing of registers.
  455.  
  456.    The caller is responsible for making
  457.    sure that the inferior is stopped before calling the fetching routines,
  458.    or it will get garbage.  (a change from GDB version 3, in which
  459.    the caller got the value from the last stop).  */
  460.  
  461. /* Contents of the registers in target byte order.
  462.    We allocate some extra slop since we do a lot of memcpy's around `registers',
  463.    and failing-soft is better than failing hard.  */
  464. char registers[REGISTER_BYTES + /* SLOP */ 256];
  465.  
  466. /* Nonzero if that register has been fetched.  */
  467. char register_valid[NUM_REGS];
  468.  
  469. /* Indicate that registers may have changed, so invalidate the cache.  */
  470. void
  471. registers_changed ()
  472. {
  473.   int i;
  474.   for (i = 0; i < NUM_REGS; i++)
  475.     register_valid[i] = 0;
  476. }
  477.  
  478. /* Indicate that all registers have been fetched, so mark them all valid.  */
  479. void
  480. registers_fetched ()
  481. {
  482.   int i;
  483.   for (i = 0; i < NUM_REGS; i++)
  484.     register_valid[i] = 1;
  485. }
  486.  
  487. /* Copy LEN bytes of consecutive data from registers
  488.    starting with the REGBYTE'th byte of register data
  489.    into memory at MYADDR.  */
  490.  
  491. void
  492. read_register_bytes (regbyte, myaddr, len)
  493.      int regbyte;
  494.      char *myaddr;
  495.      int len;
  496. {
  497.   /* Fetch all registers.  */
  498.   int i;
  499.   for (i = 0; i < NUM_REGS; i++)
  500.     if (!register_valid[i])
  501.       {
  502.     target_fetch_registers (-1);
  503.     break;
  504.       }
  505.   if (myaddr != NULL)
  506.     memcpy (myaddr, ®isters[regbyte], len);
  507. }
  508.  
  509. /* Read register REGNO into memory at MYADDR, which must be large enough
  510.    for REGISTER_RAW_BYTES (REGNO).  Target byte-order.
  511.    If the register is known to be the size of a CORE_ADDR or smaller,
  512.    read_register can be used instead.  */
  513. void
  514. read_register_gen (regno, myaddr)
  515.      int regno;
  516.      char *myaddr;
  517. {
  518.   if (!register_valid[regno])
  519.     target_fetch_registers (regno);
  520.   memcpy (myaddr, ®isters[REGISTER_BYTE (regno)],
  521.       REGISTER_RAW_SIZE (regno));
  522. }
  523.  
  524. /* Copy LEN bytes of consecutive data from memory at MYADDR
  525.    into registers starting with the REGBYTE'th byte of register data.  */
  526.  
  527. void
  528. write_register_bytes (regbyte, myaddr, len)
  529.      int regbyte;
  530.      char *myaddr;
  531.      int len;
  532. {
  533.   /* Make sure the entire registers array is valid.  */
  534.   read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
  535.   memcpy (®isters[regbyte], myaddr, len);
  536.   target_store_registers (-1);
  537. }
  538.  
  539. /* Return the raw contents of register REGNO, regarding it as an integer.  */
  540. /* This probably should be returning LONGEST rather than CORE_ADDR.  */
  541.  
  542. CORE_ADDR
  543. read_register (regno)
  544.      int regno;
  545. {
  546.   if (!register_valid[regno])
  547.     target_fetch_registers (regno);
  548.  
  549.   return extract_address (®isters[REGISTER_BYTE (regno)],
  550.               REGISTER_RAW_SIZE(regno));
  551. }
  552.  
  553. /* Registers we shouldn't try to store.  */
  554. #if !defined (CANNOT_STORE_REGISTER)
  555. #define CANNOT_STORE_REGISTER(regno) 0
  556. #endif
  557.  
  558. /* Store VALUE, into the raw contents of register number REGNO.  */
  559. /* FIXME: The val arg should probably be a LONGEST.  */
  560.  
  561. void
  562. write_register (regno, val)
  563.      int regno;
  564.      LONGEST val;
  565. {
  566.   PTR buf;
  567.   int size;
  568.  
  569.   /* On the sparc, writing %g0 is a no-op, so we don't even want to change
  570.      the registers array if something writes to this register.  */
  571.   if (CANNOT_STORE_REGISTER (regno))
  572.     return;
  573.  
  574.   size = REGISTER_RAW_SIZE(regno);
  575.   buf = alloca (size);
  576.   store_signed_integer (buf, size, (LONGEST) val);
  577.  
  578.   /* If we have a valid copy of the register, and new value == old value,
  579.      then don't bother doing the actual store. */
  580.  
  581.   if (register_valid [regno]) 
  582.     {
  583.       if (memcmp (®isters[REGISTER_BYTE (regno)], buf, size) == 0)
  584.     return;
  585.     }
  586.   
  587.   target_prepare_to_store ();
  588.  
  589.   memcpy (®isters[REGISTER_BYTE (regno)], buf, size);
  590.  
  591.   register_valid [regno] = 1;
  592.  
  593.   target_store_registers (regno);
  594. }
  595.  
  596. /* Record that register REGNO contains VAL.
  597.    This is used when the value is obtained from the inferior or core dump,
  598.    so there is no need to store the value there.  */
  599.  
  600. void
  601. supply_register (regno, val)
  602.      int regno;
  603.      char *val;
  604. {
  605.   register_valid[regno] = 1;
  606.   memcpy (®isters[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
  607.  
  608.   /* On some architectures, e.g. HPPA, there are a few stray bits in some
  609.      registers, that the rest of the code would like to ignore.  */
  610. #ifdef CLEAN_UP_REGISTER_VALUE
  611.   CLEAN_UP_REGISTER_VALUE(regno, ®isters[REGISTER_BYTE(regno)]);
  612. #endif
  613. }
  614.  
  615. /* Will calling read_var_value or locate_var_value on SYM end
  616.    up caring what frame it is being evaluated relative to?  SYM must
  617.    be non-NULL.  */
  618. int
  619. symbol_read_needs_frame (sym)
  620.      struct symbol *sym;
  621. {
  622.   switch (SYMBOL_CLASS (sym))
  623.     {
  624.       /* All cases listed explicitly so that gcc -Wall will detect it if
  625.      we failed to consider one.  */
  626.     case LOC_REGISTER:
  627.     case LOC_ARG:
  628.     case LOC_REF_ARG:
  629.     case LOC_REGPARM:
  630.     case LOC_REGPARM_ADDR:
  631.     case LOC_LOCAL:
  632.     case LOC_LOCAL_ARG:
  633.     case LOC_BASEREG:
  634.     case LOC_BASEREG_ARG:
  635.       return 1;
  636.  
  637.     case LOC_UNDEF:
  638.     case LOC_CONST:
  639.     case LOC_STATIC:
  640.     case LOC_TYPEDEF:
  641.  
  642.     case LOC_LABEL:
  643.       /* Getting the address of a label can be done independently of the block,
  644.      even if some *uses* of that address wouldn't work so well without
  645.      the right frame.  */
  646.  
  647.     case LOC_BLOCK:
  648.     case LOC_CONST_BYTES:
  649.     case LOC_OPTIMIZED_OUT:
  650.       return 0;
  651.     }
  652.   return 1;
  653. }
  654.  
  655. /* Given a struct symbol for a variable,
  656.    and a stack frame id, read the value of the variable
  657.    and return a (pointer to a) struct value containing the value. 
  658.    If the variable cannot be found, return a zero pointer.
  659.    If FRAME is NULL, use the selected_frame.  */
  660.  
  661. value
  662. read_var_value (var, frame)
  663.      register struct symbol *var;
  664.      FRAME frame;
  665. {
  666.   register value v;
  667.   struct frame_info *fi;
  668.   struct type *type = SYMBOL_TYPE (var);
  669.   CORE_ADDR addr;
  670.   register int len;
  671.  
  672.   v = allocate_value (type);
  673.   VALUE_LVAL (v) = lval_memory;    /* The most likely possibility.  */
  674.   len = TYPE_LENGTH (type);
  675.  
  676.   if (frame == 0) frame = selected_frame;
  677.  
  678.   switch (SYMBOL_CLASS (var))
  679.     {
  680.     case LOC_CONST:
  681.       /* Put the constant back in target format.  */
  682.       store_signed_integer (VALUE_CONTENTS_RAW (v), len,
  683.                 (LONGEST) SYMBOL_VALUE (var));
  684.       VALUE_LVAL (v) = not_lval;
  685.       return v;
  686.  
  687.     case LOC_LABEL:
  688.       /* Put the constant back in target format.  */
  689.       store_address (VALUE_CONTENTS_RAW (v), len, SYMBOL_VALUE_ADDRESS (var));
  690.       VALUE_LVAL (v) = not_lval;
  691.       return v;
  692.  
  693.     case LOC_CONST_BYTES:
  694.       {
  695.     char *bytes_addr;
  696.     bytes_addr = SYMBOL_VALUE_BYTES (var);
  697.     memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
  698.     VALUE_LVAL (v) = not_lval;
  699.     return v;
  700.       }
  701.  
  702.     case LOC_STATIC:
  703.       addr = SYMBOL_VALUE_ADDRESS (var);
  704.       break;
  705.  
  706.     case LOC_ARG:
  707.       fi = get_frame_info (frame);
  708.       if (fi == NULL)
  709.     return 0;
  710.       addr = FRAME_ARGS_ADDRESS (fi);
  711.       if (!addr)
  712.     {
  713.       return 0;
  714.     }
  715.       addr += SYMBOL_VALUE (var);
  716.       break;
  717.  
  718.     case LOC_REF_ARG:
  719.       fi = get_frame_info (frame);
  720.       if (fi == NULL)
  721.     return 0;
  722.       addr = FRAME_ARGS_ADDRESS (fi);
  723.       if (!addr)
  724.     {
  725.       return 0;
  726.     }
  727.       addr += SYMBOL_VALUE (var);
  728.       addr = read_memory_unsigned_integer
  729.     (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
  730.       break;
  731.  
  732.     case LOC_LOCAL:
  733.     case LOC_LOCAL_ARG:
  734.       fi = get_frame_info (frame);
  735.       if (fi == NULL)
  736.     return 0;
  737.       addr = FRAME_LOCALS_ADDRESS (fi);
  738.       addr += SYMBOL_VALUE (var);
  739.       break;
  740.  
  741.     case LOC_BASEREG:
  742.     case LOC_BASEREG_ARG:
  743.       {
  744.     char buf[MAX_REGISTER_RAW_SIZE];
  745.     get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
  746.                 NULL);
  747.     addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
  748.     addr += SYMBOL_VALUE (var);
  749.     break;
  750.       }
  751.                 
  752.     case LOC_TYPEDEF:
  753.       error ("Cannot look up value of a typedef");
  754.       break;
  755.  
  756.     case LOC_BLOCK:
  757.       VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
  758.       return v;
  759.  
  760.     case LOC_REGISTER:
  761.     case LOC_REGPARM:
  762.     case LOC_REGPARM_ADDR:
  763.       {
  764.     struct block *b;
  765.  
  766.     if (frame == NULL)
  767.       return 0;
  768.     b = get_frame_block (frame);
  769.     
  770.     v = value_from_register (type, SYMBOL_VALUE (var), frame);
  771.  
  772.     if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
  773.       {
  774.         addr = *(CORE_ADDR *)VALUE_CONTENTS (v);
  775.         VALUE_LVAL (v) = lval_memory;
  776.       }
  777.     else
  778.       return v;
  779.       }
  780.       break;
  781.  
  782.     case LOC_OPTIMIZED_OUT:
  783.       VALUE_LVAL (v) = not_lval;
  784.       VALUE_OPTIMIZED_OUT (v) = 1;
  785.       return v;
  786.  
  787.     default:
  788.       error ("Cannot look up value of a botched symbol.");
  789.       break;
  790.     }
  791.  
  792.   VALUE_ADDRESS (v) = addr;
  793.   VALUE_LAZY (v) = 1;
  794.   return v;
  795. }
  796.  
  797. /* Return a value of type TYPE, stored in register REGNUM, in frame
  798.    FRAME. */
  799.  
  800. value
  801. value_from_register (type, regnum, frame)
  802.      struct type *type;
  803.      int regnum;
  804.      FRAME frame;
  805. {
  806.   char raw_buffer [MAX_REGISTER_RAW_SIZE];
  807.   CORE_ADDR addr;
  808.   int optim;
  809.   value v = allocate_value (type);
  810.   int len = TYPE_LENGTH (type);
  811.   char *value_bytes = 0;
  812.   int value_bytes_copied = 0;
  813.   int num_storage_locs;
  814.   enum lval_type lval;
  815.  
  816.   VALUE_REGNO (v) = regnum;
  817.  
  818.   num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
  819.               ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
  820.               1);
  821.  
  822.   if (num_storage_locs > 1
  823. #ifdef GDB_TARGET_IS_H8500
  824.       || TYPE_CODE (type) == TYPE_CODE_PTR
  825. #endif
  826.       )
  827.     {
  828.       /* Value spread across multiple storage locations.  */
  829.       
  830.       int local_regnum;
  831.       int mem_stor = 0, reg_stor = 0;
  832.       int mem_tracking = 1;
  833.       CORE_ADDR last_addr = 0;
  834.       CORE_ADDR first_addr = 0;
  835.  
  836.       value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
  837.  
  838.       /* Copy all of the data out, whereever it may be.  */
  839.  
  840. #ifdef GDB_TARGET_IS_H8500
  841. /* This piece of hideosity is required because the H8500 treats registers
  842.    differently depending upon whether they are used as pointers or not.  As a
  843.    pointer, a register needs to have a page register tacked onto the front.
  844.    An alternate way to do this would be to have gcc output different register
  845.    numbers for the pointer & non-pointer form of the register.  But, it
  846.    doesn't, so we're stuck with this.  */
  847.  
  848.       if (TYPE_CODE (type) == TYPE_CODE_PTR
  849.       && len > 2)
  850.     {
  851.       int page_regnum;
  852.  
  853.       switch (regnum)
  854.         {
  855.         case R0_REGNUM: case R1_REGNUM: case R2_REGNUM: case R3_REGNUM:
  856.           page_regnum = SEG_D_REGNUM;
  857.           break;
  858.         case R4_REGNUM: case R5_REGNUM:
  859.           page_regnum = SEG_E_REGNUM;
  860.           break;
  861.         case R6_REGNUM: case R7_REGNUM:
  862.           page_regnum = SEG_T_REGNUM;
  863.           break;
  864.         }
  865.  
  866.       value_bytes[0] = 0;
  867.       get_saved_register (value_bytes + 1,
  868.                   &optim,
  869.                   &addr,
  870.                   frame,
  871.                   page_regnum,
  872.                   &lval);
  873.  
  874.       if (lval == lval_register)
  875.         reg_stor++;
  876.       else
  877.         mem_stor++;
  878.       first_addr = addr;
  879.       last_addr = addr;
  880.  
  881.       get_saved_register (value_bytes + 2,
  882.                   &optim,
  883.                   &addr,
  884.                   frame,
  885.                   regnum,
  886.                   &lval);
  887.  
  888.       if (lval == lval_register)
  889.         reg_stor++;
  890.       else
  891.         {
  892.           mem_stor++;
  893.           mem_tracking = mem_tracking && (addr == last_addr);
  894.         }
  895.       last_addr = addr;
  896.     }
  897.       else
  898. #endif                /* GDB_TARGET_IS_H8500 */
  899.     for (local_regnum = regnum;
  900.          value_bytes_copied < len;
  901.          (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
  902.           ++local_regnum))
  903.       {
  904.         get_saved_register (value_bytes + value_bytes_copied,
  905.                 &optim,
  906.                 &addr,
  907.                 frame,
  908.                 local_regnum,
  909.                 &lval);
  910.  
  911.         if (regnum == local_regnum)
  912.           first_addr = addr;
  913.         if (lval == lval_register)
  914.           reg_stor++;
  915.         else
  916.           {
  917.         mem_stor++;
  918.           
  919.         mem_tracking =
  920.           (mem_tracking
  921.            && (regnum == local_regnum
  922.                || addr == last_addr));
  923.           }
  924.         last_addr = addr;
  925.       }
  926.  
  927.       if ((reg_stor && mem_stor)
  928.       || (mem_stor && !mem_tracking))
  929.     /* Mixed storage; all of the hassle we just went through was
  930.        for some good purpose.  */
  931.     {
  932.       VALUE_LVAL (v) = lval_reg_frame_relative;
  933.       VALUE_FRAME (v) = FRAME_FP (frame);
  934.       VALUE_FRAME_REGNUM (v) = regnum;
  935.     }
  936.       else if (mem_stor)
  937.     {
  938.       VALUE_LVAL (v) = lval_memory;
  939.       VALUE_ADDRESS (v) = first_addr;
  940.     }
  941.       else if (reg_stor)
  942.     {
  943.       VALUE_LVAL (v) = lval_register;
  944.       VALUE_ADDRESS (v) = first_addr;
  945.     }
  946.       else
  947.     fatal ("value_from_register: Value not stored anywhere!");
  948.  
  949.       VALUE_OPTIMIZED_OUT (v) = optim;
  950.  
  951.       /* Any structure stored in more than one register will always be
  952.      an integral number of registers.  Otherwise, you'd need to do
  953.      some fiddling with the last register copied here for little
  954.      endian machines.  */
  955.  
  956.       /* Copy into the contents section of the value.  */
  957.       memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
  958.  
  959.       /* Finally do any conversion necessary when extracting this
  960.          type from more than one register.  */
  961. #ifdef REGISTER_CONVERT_TO_TYPE
  962.       REGISTER_CONVERT_TO_TYPE(regnum, type, VALUE_CONTENTS_RAW(v));
  963. #endif
  964.       return v;
  965.     }
  966.  
  967.   /* Data is completely contained within a single register.  Locate the
  968.      register's contents in a real register or in core;
  969.      read the data in raw format.  */
  970.  
  971.   get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
  972.   VALUE_OPTIMIZED_OUT (v) = optim;
  973.   VALUE_LVAL (v) = lval;
  974.   VALUE_ADDRESS (v) = addr;
  975.  
  976.   /* Convert raw data to virtual format if necessary.  */
  977.   
  978. #ifdef REGISTER_CONVERTIBLE
  979.   if (REGISTER_CONVERTIBLE (regnum))
  980.     {
  981.       REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
  982.                    raw_buffer, VALUE_CONTENTS_RAW (v));
  983.     }
  984.   else
  985. #endif
  986.     {
  987.       /* Raw and virtual formats are the same for this register.  */
  988.  
  989. #if TARGET_BYTE_ORDER == BIG_ENDIAN
  990.       if (len < REGISTER_RAW_SIZE (regnum))
  991.     {
  992.         /* Big-endian, and we want less than full size.  */
  993.       VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
  994.     }
  995. #endif
  996.  
  997.       memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
  998.     }
  999.   
  1000.   return v;
  1001. }
  1002.  
  1003. /* Given a struct symbol for a variable or function,
  1004.    and a stack frame id, 
  1005.    return a (pointer to a) struct value containing the properly typed
  1006.    address.  */
  1007.  
  1008. value
  1009. locate_var_value (var, frame)
  1010.      register struct symbol *var;
  1011.      FRAME frame;
  1012. {
  1013.   CORE_ADDR addr = 0;
  1014.   struct type *type = SYMBOL_TYPE (var);
  1015.   value lazy_value;
  1016.  
  1017.   /* Evaluate it first; if the result is a memory address, we're fine.
  1018.      Lazy evaluation pays off here. */
  1019.  
  1020.   lazy_value = read_var_value (var, frame);
  1021.   if (lazy_value == 0)
  1022.     error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
  1023.  
  1024.   if (VALUE_LAZY (lazy_value)
  1025.       || TYPE_CODE (type) == TYPE_CODE_FUNC)
  1026.     {
  1027.       addr = VALUE_ADDRESS (lazy_value);
  1028.       return value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
  1029.     }
  1030.  
  1031.   /* Not a memory address; check what the problem was.  */
  1032.   switch (VALUE_LVAL (lazy_value)) 
  1033.     {
  1034.     case lval_register:
  1035.     case lval_reg_frame_relative:
  1036.       error ("Address requested for identifier \"%s\" which is in a register.",
  1037.          SYMBOL_SOURCE_NAME (var));
  1038.       break;
  1039.  
  1040.     default:
  1041.       error ("Can't take address of \"%s\" which isn't an lvalue.",
  1042.          SYMBOL_SOURCE_NAME (var));
  1043.       break;
  1044.     }
  1045.   return 0;  /* For lint -- never reached */
  1046. }
  1047.